from mainfunc.baseapi import BaseApi
from config.fileName import title_dic_zd


class HandleFilezd(BaseApi):
    def __init__(self):
        # self.url = "http://www.chinaclear.cn/zdjs/jgszb/service_list.shtml"
        # self.req = BaseApi.getSource(self.url)
        self.file_name2 = None
        self.list = [
            'http://www.chinaclear.cn/zdjs/jgszb/service_list.shtml',
            'http://www.chinaclear.cn/zdjs/jshsc/service_list.shtml',
            'http://www.chinaclear.cn/zdjs/jszsc/service_list.shtml'
        ]

    def downF(self):
        for file_name in list(title_dic_zd.keys()):
            if '登记结算数据接口规范' == file_name:
                self.file_name2 = f'{file_name}（结算参与人版'
                url = self.list[1]
                self.req = BaseApi.getSource(url)
                partten = f'''<li>- <a href="(\S*?)" target="_blank" title='{self.file_name2}'''
                try:
                    self.download_name = BaseApi.searchName(partten, self.req)
                    fileTpye = self.download_name.split('.')[-1]
                    BaseApi.cleanFile(title_dic_zd[file_name])
                    BaseApi.downFile(fileTpye, file_name, self.download_name)
                    print(f'文件:{file_name}处理成功')
                except Exception as e:
                    print(f'文件:{file_name}处理失败,错误信息：{e}')
            if '深市登记结算数据接口规范' == file_name:
                self.file_name2 = f'{file_name}（结算参与人版'
                url = self.list[2]
                self.req = BaseApi.getSource(url)
                partten = f'''<li>- <a href="(\S*?)" target="_blank" title='TS.*?{self.file_name2}'''
                try:
                    self.download_name = BaseApi.searchName(partten, self.req)
                    fileTpye = self.download_name.split('.')[-1]
                    BaseApi.cleanFile(title_dic_zd[file_name])
                    BaseApi.downFile(fileTpye, file_name, self.download_name)
                    print(f'文件:{file_name}处理成功')
                except Exception as e:
                    print(f'文件:{file_name}处理失败,错误信息：{e}')
            if '担保品管理系统数据接口规范' == file_name:
                url = self.list[0]
                self.req = BaseApi.getSource(url)
                partten = f'''<li>- <a href="(\S*?)" target="_blank" title='{file_name}（'''
                try:
                    self.download_name = BaseApi.searchName(partten, self.req)
                    fileTpye = self.download_name.split('.')[-1]
                    BaseApi.cleanFile(title_dic_zd[file_name])
                    BaseApi.downFile(fileTpye, file_name, self.download_name)
                    print(f'文件:{file_name}处理成功')
                except Exception as e:
                    print(f'文件:{file_name}处理失败,错误信息：{e}')
